<?php
class NodeAction extends CommonAction {

	//自定义单一模块无须认证的方法
	public function _noAuth()
	{
		//默认方法index,getlist是模块认证后无须认证的
		if(in_array(ACTION_NAME,array('index','getlist','treeArray','selectIcon'))) return true;
		else return false;
	}
	//暂时没有用到filter
	public function filter(&$map)
	{
        if(!empty($_GET['group_id'])) {
            $map['group_id'] =  $_GET['group_id'];
            $this->assign('nodeName','分组');
        }elseif(empty($_POST['search']) && !isset($map['pid']) ) {
			$map['pid']	=	0;
		}
		if($_GET['pid']!=''){
			$map['pid']=$_GET['pid'];
		}
		$_SESSION['currentNodeId']	=	$map['pid'];
		//获取上级节点
		$node  = M("Node");
        if(isset($map['pid'])) {
            if($node->getById($map['pid'])) {
                $this->assign('level',$node->level+1);
                $this->assign('nodeName',$node->name);
            }else {
                $this->assign('level',1);
            }
        }
	}
	
	public function _before_index() {
		$this->_getButton();
		$model	=	M("Group");
		$list	=	$model->where('status=1')->field('id,title as text')->select();
		//$list	=	$model->where('status=1')->getField('id,title');
		$this->assign('groupList',$list);
	}

	// 获取配置类型
	public function _before_add() {
		if (!$this->isPost()){ 
			$model	=	M("Group");
			$list	=	$model->where('status=1')->field('id,title as text')->select();//下拉
			$this->assign('list',$list);
			$node	=	M("Node");
			//	$node->getById($_SESSION['currentNodeId']);
			$node->getById($_REQUEST['pid']);
			$this->assign('pid',$node->id);
			$this->assign('level',$node->level+1);
			$this->assign('ptitle',$node->title);
		}
	
	}

    public function _before_patch() {
		$model	=	M("Group");
		$list	=	$model->where('status=1')->select();
		$this->assign('list',$list);
		$node	=	M("Node");
		$node->getById($_SESSION['currentNodeId']);
        $this->assign('pid',$node->id);
		$this->assign('level',$node->level+1);
    }
	public function _before_edit() {
		if (!$this->isPost()){
			$model	=	M("Group");
			$list	=	$model->where('status=1')->select();
			$this->assign('list',$list);
		}
		
	}

    //排序功能
    public function sort()
    {
		$node = M('Node');
        if(!empty($_GET['sortId'])) {
            $map = array();
            $map['status'] = 1;
            $map['id']   = array('in',$_GET['sortId']);
            $sortList   =   $node->where($map)->order('sort asc')->select();
        }else{
            if(!empty($_GET['pid'])) {
                $pid  = $_GET['pid'];
            }else {
                $pid  = $_SESSION['currentNodeId'];
            }
            if($node->getById($pid)) {
                $level   =  $node->level+1;
            }else {
                $level   =  1;
            }
            $this->assign('level',$level);
            $sortList   =   $node->where('status=1 and pid='.$pid.' and level='.$level)->order('sort asc')->select();
        }
        $this->assign("sortList",$sortList);
        $this->display();
        return ;
    }
    //选择图标
    public function selectIcon() {
    	$document_root =str_replace('/', '\\', $_SERVER["DOCUMENT_ROOT"]);
    	$directory=$document_root.__ROOT__.'/Public/lib/icons/32X32';
    	$files=sdir($directory,"*.gif");
    	$this->assign('iconfile',$files);
    	$this->assign('iconpath',__ROOT__.'/Public/lib/icons/32X32/');
		$this->display();
    }
    //列表树
    public function treeArray() {
    	$name = $this->getActionName();
    	$model = D($name);
    	$map = $this->_searchRule();
    	$rows = $model->where($map)->order("pid asc,sort asc")->select();
    	$result = array();
    	$I = array();
    	//Log::write('调试的SQL语句：' . $model->getLastSql(), Log::SQL);
        if(!empty($rows)){
    		$parentid=$rows[0]['pid'];
    	}
    	else {
    		$parentid=0;
    	}
    	foreach($rows as $val) {
    		if($val['pid'] == $parentid) {
    		//if(!isset($val['_flag'])) {
    			$i = count($result);
    			$result[$i] = $val;
    			$I[$val['id']] = & $result[$i];
    		} else {
    			if(isset($I[$val['pid']]['children'])){
    				$i = count($I[$val['pid']]['children']);
    			}
    			else{
    				$i=0;
    			}
    			$I[$val['pid']]['children'][$i] = $val;
    			$I[$val['id']] = & $I[$val['pid']]['children'][$i];
    		}
    	}
    	echo json_encode(array('Rows'=>$result));
    }
    //获取妥组名称
    function getNodeGroupName() {
    	$id=$_GET['group_id'];
    	$Group = D ( "Group" );
    	$list = $Group->getField ( 'id,title' );
    	$_SESSION ['nodeGroupList'] = $list;
    	$name = $list [$id];
    	echo $name;
    }
    
    public function addButtons() {
    	$pid=$_REQUEST['id'];
    	$name = $this->getActionName();
    	$model = D($name);
    	$data['pid']=$pid;
    	$data['status']=1;
    	$data['level']=3;
    	$data['is_show']=1;
    	
    	$data['name']='add';
    	$data['title']='增加';
    	$data['nodeicon']="/Public/lib/ligerUI/skins/icons/add.gif";
    	$data['sort']=1;
    	$list = $model->data($data)->add();

    	$data['name']='edit';
    	$data['title']='编辑';
    	$data['nodeicon']="/Public/lib/ligerUI/skins/icons/modify.gif";
    	$data['sort']=2;
    	$list = $model->data($data)->add();
    	
    	$data['name']='del';
    	$data['title']='删除';
    	$data['nodeicon']="/Public/lib/ligerUI/skins/icons/delete.gif";
    	$data['sort']=3;
    	$list = $model->data($data)->add();
    	
    	$data['name']='view';
    	$data['title']='查看';
    	$data['nodeicon']="/Public/lib/ligerUI/skins/icons/archives.gif";
    	$data['sort']=4;
    	$list = $model->data($data)->add();
    	if ($list !== false) { //保存成功
    		$this->success('批量新增按钮成功!');
    	} else {
    		//失败提示
    		$this->error('批量新增按钮失败!');
   	
    	}
    	
    }
   
}
?>